package com.ljh.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.ljh.common.JsonData;
import com.ljh.common.Memconstant;
import com.ljh.pojo.Client;
import com.ljh.pojo.Member;
import com.ljh.mapper.MemberMapper;
import com.ljh.service.MEMBERervice;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljh.utils.ExceptionUtils;
import com.ljh.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘剑鸿
 * @since 2021-11-23
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MEMBERervice {
    @Autowired
    private MemberMapper memberMapper;
    public JsonData getloadAll(MemberVo memberVo){
        Page<Member>page = PageHelper.startPage(memberVo.getPage(),memberVo.getLimit());
        List<Member> MemberList = memberMapper.selectMember(memberVo);
        if(CollectionUtils.isEmpty(MemberList)){
            MemberList = Lists.newArrayList();
        }
        return JsonData.success(page.getTotal(),MemberList);
    }
    public void addMember(Member member){
        member.setIntegral(Memconstant.INTEGRAL);
        member.setBalance(Memconstant.BALANCE);
        member.setMemberGradeid(Memconstant.MEMBER_GRADEID);
        member.setDeleted(Memconstant.DELETED);
        int rows = memberMapper.insertMember(member);
        if(rows == 0){
            throw new ExceptionUtils("新增用户失败，插入数据库出错");
        }
    }
    public List<Member> getListmem(){
        return memberMapper.getGrade();
    }
    public Member getmemberId(Integer id){
        return memberMapper.getmemberId(id);
    }

    public int delectmemId(Integer id){
        return memberMapper.deleteById(id);
    }
    public void updataIntSave(Member member){
        int row = memberMapper.ToupdateInte(member);
        if (row == 0){
            throw new ExceptionUtils("添加积分失败，不成功");
        }
    }

    public void updataSave(Member member, Client client){
        Member member1 = memberMapper.getmemberId(member.getMemberid());
        if (member1 == null){
            throw new ExceptionUtils("修改会员失败，不存在");
        }
        client.setClientid(member.getClientid());
        client.setClientname(member.getClientname());
        client.setPhoneNumber(member.getPhoneNumber());
        int row1 = memberMapper.ToupdateClient(client);
        int rows = memberMapper.ToupdateSave(member);
        if (rows == 0 || row1 == 0){
            throw new ExceptionUtils("修改会员失败，不成功");
        }
    }
    public void updatabalance(Member member){
        int row = memberMapper.ToupdateBalance(member);
        if (row == 0 ){
            throw new ExceptionUtils("充值失败");
        }
    }
}
